package Sort.面试.腾讯;

/**
 * 给定一个只包含'0'和'1'两种字符的字符串，每次操作可以选择相邻的两个字符，将它们同时变成'0'或者同时变成'1'。
 * 请问最少多少次操作后，所有的字符都相同？
 */
public class MinOperations2 {
    public static int minOperations (String str) {
        // write code here
        if (str == null || str.length() <= 1) {
            return 0;
        }
        int len = str.length();
        int zero = 0;
        int dZreo = 0;
        int one = 0;
        int dOne = 0;

        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);
            if (ch == '0'){
                zero ++;
            }else{
                one ++;
            }
        }

        String temp1 = str;
        String temp2 = str;
        while(temp1.contains("00") || temp2.contains("11")){
            while(temp1.contains("00")){
                temp1 = temp1.replaceFirst("00","");
                dZreo ++;
            }
            while(temp2.contains("11")){
                temp2 = temp2.replaceFirst("11", "");
                dOne ++;
            }
        }

        int oneSize = one - dOne;
        int zeroSize = zero - dZreo;

        return oneSize <= zeroSize ? oneSize : zeroSize;
    }

    public static void main(String[] args) {
        String str = "01001";
        System.out.println(minOperations(str));
    }
}
